package edu.rakovic.elearning.beans;

import edu.rakovic.elearning.exceptions.RedudantAppliacationException;
import edu.rakovic.elearning.interfaces.Application;
import edu.rakovic.elearning.interfaces.Course;
import edu.rakovic.elearning.interfaces.User;
import edu.rakovic.elearning.service.Applications;

public class ApplicationBean implements Application {
	
	private String id;
	private User user;
	private Course course;
	
	public void setId(String id) {
		this.id = id;
	}
	public String getId() {
		return id;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public User getUser() {
		return user;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
	public Course getCourse() {
		return course;
	}
	
	public Application applyCourse(User user, Course course) throws RedudantAppliacationException{
		
		if(!user.isAppliedToCourse(course)){		
			user.getCourseApplication().add(this);
			course.getUserApplication().add(this);
			this.setCourse(course);
			this.setUser(user);
			return this;
		}else 
			throw new RedudantAppliacationException("You are already applied to course!");
		

	
	}
	
	public Application cancelApplication(Applications applications){
			//removing connections with other classes
			user.getCourseApplication().remove(this);
			course.getUserApplication().remove(this);
			//... and delete application from "database"
			applications.delete(this.getId());
			
			return this;
		
	}
	
}
